1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.smartcrawler.filter;
28 import java.util.Collection;
29 import java.util.Iterator;
30 import java.util.Vector;
31 import org.apache.log4j.Logger;
32 import org.smartcrawler.common.Context;
33 import org.smartcrawler.common.Link;
34 import org.smartcrawler.common.SCLogger;
35 import org.smartcrawler.retriever.Content;
36
37
38 /***
39 *
40 *
41 * @author <a href="mailto:pozzad@alice.it">Davide Pozza</a>
42 * @version <tt>$Revision: 1.7 $</tt>
43 */
44 public class FilterManager {
45
46 private static Logger log = SCLogger.getLogger(FilterManager.class);
47 private static Logger logPerm = SCLogger.getPermissionLogger();
48 private static FilterManager fm = null;
49 private PrecFilterLink[] precFilters;
50 private PostFilterLink[] postFilters;
51
52 /*** Creates a new instance of FilterManager */
53 public FilterManager(Collection filters) {
54 loadFilters(filters);
55 log.debug("FilterManager(): filters loaded: " + filters.size());
56 }
57
58 /*** Creates a new instance of FilterManager */
59 public FilterManager() {
60 }
61
62 /***
63 *
64 * @param filters
65 */
66 public void addPrecFilters(Collection filters) {
67 Iterator i = filters.iterator();
68 Vector tmpVectPrec = new Vector();
69 while (i.hasNext()) {
70 Object value = i.next();
71 if (value instanceof String) {
72 String cls = "" + value;
73 Object oCls = null;
74 try {
75 oCls = Class.forName(cls).newInstance();
76 } catch (Exception e) {
77 log.warn("loadFilters(): unable to load a new instance of "
78 + cls + ": ignoring");
79 }
80 tmpVectPrec.add(oCls);
81 } else {
82 tmpVectPrec.add(value);
83 }
84 }
85 precFilters = new PrecFilterLink[tmpVectPrec.size()];
86 tmpVectPrec.copyInto(precFilters);
87 }
88
89 /***
90 *
91 * @param filters
92 */
93 public void addPostFilters(Collection filters) {
94 Iterator i = filters.iterator();
95 Vector tmpVectPost = new Vector();
96 while (i.hasNext()) {
97 Object value = i.next();
98 if (value instanceof String) {
99 String cls = "" + value;
100 Object oCls = null;
101 try {
102 oCls = Class.forName(cls).newInstance();
103 } catch (Exception e) {
104 log.warn("loadFilters(): unable to load a new instance of "
105 + cls + ": ignoring");
106 }
107 tmpVectPost.add(oCls);
108 } else {
109 tmpVectPost.add(value);
110 }
111 }
112
113 postFilters = new PostFilterLink[tmpVectPost.size()];
114 tmpVectPost.copyInto(postFilters);
115 }
116
117 private void loadFilters(Collection filters) {
118 Iterator i = filters.iterator();
119 Vector tmpVectPrec = new Vector();
120 Vector tmpVectPost = new Vector();
121 while (i.hasNext()) {
122 String cls = "" + i.next();
123 Object oCls = null;
124 try {
125 oCls = Class.forName(cls).newInstance();
126 } catch (Exception e){
127 log.warn("loadFilters(): unable to load a new instance of "
128 + cls + ": ignoring");
129 }
130 if (oCls instanceof PrecFilterLink) {
131 tmpVectPrec.add(oCls);
132 } else if (oCls instanceof PostFilterLink) {
133 tmpVectPost.add(oCls);
134 } else {
135 log.warn("loadFilters(): class " + cls
136 + " doesn't implements a Filter interface: ignoring");
137 }
138 }
139
140 precFilters = new PrecFilterLink[tmpVectPrec.size()];
141 tmpVectPrec.copyInto(precFilters);
142
143 postFilters = new PostFilterLink[tmpVectPost.size()];
144 tmpVectPost.copyInto(postFilters);
145 }
146
147 /***
148 *
149 * @param conf
150 * @param link
151 * @return
152 */
153 public boolean isPermitted(Context conf, Link link) {
154 if (link == null) {
155 log.warn("Unable to check a null link");
156 return false;
157 }
158 boolean res = true;
159 for (PrecFilterLink filter : precFilters) {
160 try {
161 res = res && filter.isPermitted(conf, link);
162 } catch(Exception e) {
163 res = false;
164 break;
165 }
166 }
167 logPerm.info("PREC " + link.toString() + " " + res);
168 return res;
169 }
170
171 /***
172 *
173 * @param content
174 * @return
175 */
176 public boolean isPermitted(Context conf, Content content) {
177 if (content == null) {
178 log.warn("Unable to check a null content");
179 return false;
180 }
181 boolean res = true;
182 for (PostFilterLink filter : postFilters) {
183 try {
184 res = res && filter.isPermitted(conf, content);
185 } catch(Exception e){
186 res = false;
187 break;
188 }
189 }
190 logPerm.info("POST " + content.getLink().toString() + " " + res);
191 return res;
192 }
193 }